package org.spring.boot.multiple.jdbctemplate.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author: eastcom
 * @desc: 多数据源配置
 *
 *  * @Configuration 相当于配置一个Spring上下文，相当于一个<beans></beans>
 *  * @Bean 相当于配置一个bean，其中value相当于ID
 *  * @Primary 这个是必须配置的，相当于主数据源
 *  * @ConfigurationProperties 将配置文件转化为实体类
 **/
@Configuration(value = "base")
public class DataSourceConfig {

	@Primary
	@Bean(value = "fmDataSource")
	@Qualifier("fmDataSource")
	@ConfigurationProperties( prefix = "spring.datasource.druid.fm")
	public DataSource getFmDataSource(){
		return DataSourceBuilder.create().build();
	}

	@Bean("irmsDataSource")
	@Qualifier("irmsDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.druid.irms")
	public DataSource getIrmsDataSource(){
		return DataSourceBuilder.create().build();
	}

	@Bean("fmJdbcTemplate")
	public JdbcTemplate getFmJdbcTemplate(@Qualifier("fmDataSource") DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}

	@Bean("irmsJdbcTemplate")
	public JdbcTemplate getIrmsJdbcTemplate(@Qualifier("irmsDataSource") DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}
}
